<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Account extends MX_Controller {


	function __construct(){

        parent::__construct();

		$this->load->model('account_model');
		
		$this->load->model('order/order_model');
		
		$this->load->helper(array('form', 'url'));
		
		$this->load->library('form_validation');
		
		$this->load->library('auth/tank_auth');
		
		$this->load->library('session');
		
		$this->load->library("pagination");
		
		if (!Modules::run('auth/is_login'))
			redirect('auth/login2');
	}
	
	function index(){
		$data = array();
		
		$this->load->view('account', $data);	
	}

	function edit($user_id){
		$user_id = Modules::run('auth/get_user_id');
		$username = Modules::run('auth/get_username');
		
		$customer = $this->account_model->get_customer_by_user($user_id);
		
		$this->form_validation->set_error_delimiters('<span class="error-form">', '</span>');
		$this->form_validation->set_message('required', ' %s is required ');
		$this->form_validation->set_message('is_unique', 'Username already exists. Please choose another username.');
		if($this->input->post('username') == $username ){
			$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|max_length[50]');
		}else{
			$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|max_length[50]|is_unique[users.username]');
		}
		
		$this->form_validation->set_rules('first_name', 'First Name', 'trim|required|xss_clean|max_length[50]');
		$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required|xss_clean|max_length[50]');
		$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean|valid_email');
		

		if ($this->form_validation->run() == FALSE){

			$data['hasil'] =  $this->account_model->select($user_id);
			$this->load->view('account_edit', $data);

		}else{
			$this->account_model->update_user($user_id);
			redirect('account');
		}
	}
	
	function address(){
		$user_id = Modules::run('auth/get_user_id');
		
		$data['address'] = $this->account_model->get_customer_by_user($user_id);
		if(empty($data['address'])){
			$this->load->view('info_empty');
		}else{
			$this->load->view('address', $data);
		}
	}
	
	function address_edit($customer_id){
		$user_id = Modules::run('auth/get_user_id');
		
		$customer = $this->account_model->get_customer_by_user($user_id);
		
		$this->form_validation->set_error_delimiters('<span class="error-form">', '</span>');
		$this->form_validation->set_message('required', ' %s is required ');
		
		$this->form_validation->set_rules('address1', 'Address 1', 'trim|required|xss_clean|max_length[200]');
		$this->form_validation->set_rules('address2', 'Address 2', 'trim|required|xss_clean|max_length[200]');
		$this->form_validation->set_rules('telephone', 'Telephone', 'trim|required|xss_clean|numeric|max_length[30]');
		$this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean|max_length[100]');
		$this->form_validation->set_rules('post_code', 'Post Code', 'trim|required|xss_clean|numeric|max_length[100]');
		$this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean|max_length[100]');
		$this->form_validation->set_rules('region', 'region', 'trim|required|xss_clean|max_length[100]');
		
		if($this->form_validation->run() == FALSE){
			
			$data['get_country'] = $this->account_model->get_country();
		
			$data['address'] = $this->account_model->get_customer_by_user($user_id);
		
			$this->load->view('address_edit', $data);
		}else{
			
			$this->account_model->update_address($user_id);
			
			redirect('account');
		}
	}
	
	function get_region($country_id){

		$data['region'] = $this->account_model->get_region_by_country_id($country_id);

		$this->load->view('checkout/get_region', $data);

	}
	
	function order(){
		$user_id = Modules::run('auth/get_user_id');
		
		$customer = $this->order_model->get_customer_by_user($user_id);
		if($customer != NULL){
		$ord = $this->account_model->get_order_by_customer($customer->customer_id);
		}
		//var_dump($ord);
		
		if($customer == NULL || $ord == NULL){
			$this->load->view('order_empty');
		}else{
		
		$offset = $this->uri->segment(3);
		$limit = '10';
		if(!$offset){
			$page = 0;
		}else{
			$page = $offset; 	
		}
		
		$data['order'] = $this->order_model->get_order_by_customer($customer->customer_id, $page, $limit);
		//var_dump($data['order']);
		$config_pagination['base_url']    = site_url('account/order');
		$config_pagination['total_rows']  = count( $this->order_model->get_order_by_customer($customer->customer_id, 0, 0));
		$config_pagination['per_page']    = $limit;
		$config_pagination['uri_segment'] = 3;
		$config_pagination['num_links']   = 4;

		//pagination style
		$config_pagination['full_tag_open'] = '<div class="pagination"><ul>';
		$config_pagination['full_tag_close'] = '</ul></div>';

		$config_pagination['first_link'] = 'First';
		$config_pagination['first_tag_open']  = '<li>';
		$config_pagination['first_tag_close'] = '</li>';

		$config_pagination['last_link'] = 'Last';
		$config_pagination['last_tag_open']  = '<li>';
		$config_pagination['last_tag_close'] = '</li>';

		$config_pagination['next_link'] = '&gt;';
		$config_pagination['next_tag_open']  = '<li>';
		$config_pagination['next_tag_close'] = '</li>';

		$config_pagination['prev_link'] = '&lt;';
		$config_pagination['prev_tag_open'] = '<li>';
		$config_pagination['prev_tag_close'] = '</li>';

		$config_pagination['cur_tag_open'] = '<li class="disabled"><a href="#">';
		$config_pagination['cur_tag_close'] = '</a></li>';

		$config_pagination['num_tag_open'] = '<li>';
		$config_pagination['num_tag_close'] = '</li>';

		$this->pagination->initialize($config_pagination); 

		$data['pagination'] = $this->pagination->create_links();
		
		$this->load->view('order_history', $data);
		}
	}
	
	function order_info($order_cid = ''){
		$user_id = Modules::run('auth/get_user_id');
		
		$data['order'] = $this->order_model->get_order_by_cid($order_cid);
		
		$this->load->view('order_history_info', $data);
	}
	
	function order_info_id($order_id = ''){
		$user_id = Modules::run('auth/get_user_id');
		
		$data['order'] = $this->account_model->get_order_by_id($order_id);
		
		$data['order_product'] = $this->account_model->get_order_product_by_id($order_id);
		
		$data['order_total'] = $this->account_model->get_order_total_by_id($order_id);
		
		$data['order_history'] = $this->account_model->get_history($order_id);
		
		$this->load->view('order_history_info_id', $data);
	}
	
	function address_create(){
		$user_id = Modules::run('auth/get_user_id');
		
		$customer = $this->account_model->get_customer_by_user($user_id);
		
		$this->form_validation->set_error_delimiters('<span class="error-form">', '</span>');
		$this->form_validation->set_message('required', ' %s is required ');
		
		$this->form_validation->set_rules('address1', 'Address 1', 'trim|required|xss_clean|max_length[200]');
		$this->form_validation->set_rules('address2', 'Address 2', 'trim|required|xss_clean|max_length[200]');
		$this->form_validation->set_rules('telephone', 'Telephone', 'trim|required|xss_clean|numeric|max_length[30]');
		$this->form_validation->set_rules('city', 'City', 'trim|required|xss_clean|max_length[100]');
		$this->form_validation->set_rules('post_code', 'Post Code', 'trim|required|xss_clean|numeric|max_length[100]');
		$this->form_validation->set_rules('country', 'Country', 'trim|required|xss_clean|max_length[100]');
		$this->form_validation->set_rules('region', 'region', 'trim|required|xss_clean|max_length[100]');
		
		if($this->form_validation->run() == FALSE){
			
			$data['get_country'] = $this->account_model->get_country();
		
			$this->load->view('address_create', $data);
		}else{
			
			$this->account_model->create_address();
			
			redirect('account/address');
		}
	}
}
